<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {pp_inputcheckboxall} function plugin
 *
 * File:       function.pp_inputcheckboxall.php<br>
 * Type:       function<br>
 * Name:       pp_inputcheckboxall<br>
 * Date:       24.Feb.2003<br>
 * Purpose:    Prints out a list of checkbox input types<br>
 * Input:<br>
 *           - name       (optional) - string default "checkbox"
 *           - value     (required) - array
 *           - options    (optional) - associative array
 *           - checked    (optional) - array default not set
 *           - separator  (optional) - ie <br> or &nbsp;
 *           - output     (optional) - the output next to each checkbox
 *           - assign     (optional) - assign the output as an array to this variable
 * Examples:
 * <pre>
 * {pp_inputcheckboxall values=$ids output=$names}
 * {pp_inputcheckboxall values=$ids name='box' separator='<br>' output=$names}
 * {pp_inputcheckboxall values=$ids checked=$checked separator='<br>' output=$names}
 * </pre>
 * @link http://smarty.php.net/manual/en/language.function.html.inputcheckbox.php {pp_inputcheckboxall}
 *      (Smarty online manual)
 * @author     Christopher Kvarme <christopher.kvarme@flashjab.com>
 * @author credits to Monte Ohrt <monte at ohrt dot com>
 * @version    1.0
 * @param array
 * @param Smarty
 * @return string
 * @uses smarty_function_escape_special_chars()
 */
function smarty_function_pp_inputcheckboxall($params, &$smarty)
{
    require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');

    $name = NULL;
    $value = NULL;
	$values = NULL;
    $selected = NULL;
	$target = NULL;
    $separator = '';
    $label = true;
    $output = NULL;
	$onclick = NULL;

    $extra = '';

    foreach($params as $_key => $_val) {
        switch($_key) {
            case 'name':
            case 'separator':
                $$_key = $_val;
                break;

            case 'label':
                $$_key = (bool)$_val;
                break;

            case 'option':
                $$_key = (array)$_val;
                break;

            case 'value':
            case 'output':
                $$_key = (string)$_val;
                break;
				
            case 'values':
                $$_key = (string)$_val;
				break;
				
            case 'onclick':
                $$_key = (string)$_val;
				break;
				
            case 'output':
                $$_key = array_values((array)$_val);
                break;

            case 'checked':
            case 'selected':
                $selected = array_map('strval', array_values((array)$_val));
                break;
			
			case 'class':
				break;

            case 'inputcheckbox':
                $smarty->trigger_error('pp_inputcheckboxall: the use of the "inputcheckbox" attribute is deprecated, use "options" instead', E_USER_WARNING);
                $options = (array)$_val;
                break;

            case 'assign':
                break;

            default:
                if(!is_array($_val)) {
                    $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
                } else {
                    $smarty->trigger_error("pp_inputcheckboxall: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }

	//
	$_html_result = smarty_function_pp_inputcheckboxall_output($name, $value, $values, $selected, $onclick, $extra, $separator, $label);
	
	return $_html_result;
}

function smarty_function_pp_inputcheckboxall_output($name, $value, $values, $selected, $onclick, $extra, $separator, $label) {
    $_output = '';
	
	if( !isset($name) ) {
		$name = 'sla';
	}
	if( !isset($values) || $values == '' ) {
		$values = 'cb';
	}
    if ($label) $_output .= '<label>';
    $_output .= '<input type="checkbox" name="'
        . smarty_function_escape_special_chars($name) . '" value="'
        . smarty_function_escape_special_chars($value) . '"';
	
	$_output .= ' onClick="PJS.list.selectAll(this, \'' . $values . '\');';
	if( isset($onclick) && $onclick != '' ) {
		$_output .= '' . $onclick . '';
	}
	$_output .= '"';
	
    if( is_array($selected) && in_array((string)$value, $selected)) {
        $_output .= ' checked="checked"';
    }
    $_output .= $extra . '>' . $output;
    if ($label) $_output .= '</label>';
    $_output .=  $separator;

    return $_output;

}
//
?>